package com.example.mycarcalling.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationUtils;

import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.lib_cache.service.ApiService;
import com.example.lib_cache.viewmodel.StartActivityViewModel;
import com.example.lib_cache.viewmodel.WaitingActivityViewModel;
import com.example.mycarcalling.R;
import com.example.mycarcalling.databinding.ActivityWaitingBinding;

import java.util.Timer;
import java.util.TimerTask;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class WaitingActivity extends AppCompatActivity {

    private final static String TAG = WaitingActivity.class.getSimpleName();

    @Inject
    ApiService apiService;

    private Context context;

    private ActivityWaitingBinding activityWaitingBinding;

    private WaitingActivityViewModel waitingActivityViewModel;

    private Timer requestTimer = null;
    private Timer permitTimer = null;
    ProgressDialog progressDialog = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityWaitingBinding = DataBindingUtil
                .setContentView(this,R.layout.activity_waiting);
        context = this;

        initView();
        initData();
        initObservble();

    }

    private void initView(){
        setTitle("等待接单");
        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(100);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(300, 300);

        Glide.with(this)
                .asGif()
                .load(R.mipmap.waitting_car)
                .apply(options)
                .into(activityWaitingBinding.ivCarGif);

        activityWaitingBinding.tvWaitingTip.setAnimation(
                AnimationUtils.loadAnimation(this,R.anim.flash_leave_now));

    }

    private void initListener(){

    }

    private void initData(){
        //创建数据源
        //创建一个ViewModel的工厂
        ViewModelProvider.Factory factory = ViewModelProvider.
                AndroidViewModelFactory.getInstance(getApplication());
        //创建ViewModel的管理对象
        ViewModelProvider viewModelProvider = new ViewModelProvider(this,factory);
        //生产ViewModel
        waitingActivityViewModel = viewModelProvider.
                get(WaitingActivityViewModel.class);
        waitingActivityViewModel.initData();


        requestTimer = new Timer();
        permitTimer = new Timer();

        Log.d(TAG,"3秒后开始执行：每5秒请求一次");
        requestTimer.schedule(new TimerTask() {
            public void run() {
//                    Log.d(TAG,"发起请求");
                    waitingActivityViewModel.startWaiting(apiService,context);
            }
        }, 3000,5000);


        Log.d(TAG,"3000秒后停止请求");
        permitTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                Log.d(TAG,"停止请求！");
                requestTimer.cancel();
                activityWaitingBinding.ivCarGif.setVisibility(View.INVISIBLE);
//                activityWaitingBinding.tvWaitingTip.setVisibility(View.INVISIBLE);
                activityWaitingBinding.tvWaitingTip.setText("暂时没有司机接单，再等等吧~");
            }
        },300000);

    }

    private void initObservble(){
        waitingActivityViewModel.getTimerExit().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                progressDialog = new ProgressDialog(context);
                progressDialog.setIcon(R.mipmap.ic_launcher);
                progressDialog.setTitle("订单");
                if(aBoolean){
                    activityWaitingBinding.ivCarGif.setVisibility(View.INVISIBLE);
                    activityWaitingBinding.tvWaitingTip.setVisibility(View.INVISIBLE);
                    progressDialog.setIcon(R.mipmap.ic_launcher);
                    progressDialog.setTitle("订单");
                    progressDialog.setMessage("您的订单司机已接单！");
                    progressDialog.setIndeterminate(false);// 是否形成一个加载动画  true表示不明确加载进度形成转圈动画  false 表示明确加载进度
                    progressDialog.setCancelable(false);//点击返回键或者dialog四周是否关闭dialog  true表示可以关闭 false表示不可关闭
                    progressDialog.show();
                    final Timer t = new Timer();
                    t.schedule(new TimerTask() {
                        public void run() {
                            progressDialog.dismiss();
                            t.cancel();
                        }
                    }, 3000);

                    requestTimer.cancel();
                    permitTimer.cancel();

                    Log.d(TAG,"订单被司机接受");
                    //跳转地图对接导航
                    setResult(1);
                    finish();


                    return;

                }
            }
        });
    }

    @Override
    public void onStop() {
        super.onStop();
//        requestTimer.cancel();
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        if(null!=requestTimer){
            requestTimer.cancel();
        }
        if(null!=progressDialog){
            progressDialog.dismiss();
        }
    }
}